package cn.edu.flying_birds;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @author Li.Ding.yin
 * @create 2021年06月25日17:07-星期五
 * @role 柱子类
 */
public class Column {

    //定义柱子的x，y坐标
    public int x;
    public int y;
    //定义柱子的宽度和高度
    public int width;
    public int height;
    //定义柱子图片
    public BufferedImage img;
    //定义随机数
    private Random random = new Random();
    //定义两根柱子的距离
    private int distance;
    //定义柱子空隙
    public int gap;

    //无参构造
    public Column(int index) {
        try {
            //调用初始化方法
            init(index);
        } catch (IOException e) {//调用失败
            System.out.println("初始化柱子失败");
            e.printStackTrace();
        }
    }

    //初始化方法
    private void init(int num) throws IOException {
        img = ImgUtils.getImg("../img/column.png");
        width = img.getWidth() / 2;
        height = img.getHeight() / 2;
        distance = 240;
        //判断创建的是第几个柱子
        x = 300 + distance * (num == 1 ? 0 : 1);
        y = -random.nextInt(height - (644 - new Ground().height) + 1);
        gap = 75;
    }

    //柱子移动的方法
    public void move() {
        //判断柱子是否跑出窗口
        if (x <= -width) {
            x = 432;
            y = -random.nextInt(height - (644 - new Ground().height) + 1);
        }
        x--;
    }
}
